package com.hrt.day06.pattern

/**
 * 对象匹配
 * unapply方法的使用
   case People(age, name) =>
    会去调用 People 伴生对象的unapply
 */
class A(val age: Int, val name: String)

object People {
    // 参数为要传递的对象,也是match前面的变量,返回值必须是Option,而且是元组,要和解析的一样
    def unapply(pp: A): Option[(Int, String)] =
        if (pp != null)
            Some(pp.age, pp.name)
        else None
}

object ObjMatch {
    def main(args: Array[String]): Unit = {
        val p = new A(10, "zhiling")
        p match {
            case People(age, name) => println(age, name)
            case _ =>
        }
    }
}

